package com.dmx.platform.service;

import java.util.concurrent.LinkedBlockingQueue;

import org.apache.log4j.Logger;

/**
 *@author:Beni Yang
 *@date:2012-9-2
 *@function:
 */
public abstract class NotifyService extends Thread {

	public static Logger log = Logger.getLogger(NotifyService.class);
	public static LinkedBlockingQueue<Message> outingQ = new LinkedBlockingQueue<Message>();
	private static LinkedBlockingQueue<Message> persistQ = new LinkedBlockingQueue<Message>();

	private long sendInterval = 1000;
	private long getDataInterval = 60 * 1000;
	private boolean runningStatus = true;

	/**
	 *@author: Beni Yang
	 *@function:
	 *@date:2012-9-2
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public void run() {

		log.info("Starting notify service ...(Send out inverval is " + this.sendInterval / 1000 + " seconds/ Get data from database interval is " + this.getDataInterval / 1000
				+ " seconds)");

		while (runningStatus) {
			try {
				synchronized (this) {
					wait(sendInterval);
				}

				if (!outingQ.isEmpty()) {
					Message msg = outingQ.take();
					sendMessage(msg);
				}
			} catch (Exception e) {
				log.error(e.getMessage(), e);
			}
		}

	}

	protected abstract void sendMessage(Message msg);

	public long getSendInterval() {
		return sendInterval;
	}

	public void setSendInterval(long sendInterval) {
		this.sendInterval = sendInterval;
	}

	public long getGetDataInterval() {
		return getDataInterval;
	}

	public void setGetDataInterval(long getDataInterval) {
		this.getDataInterval = getDataInterval;
	}

	public boolean isRunningStatus() {
		return runningStatus;
	}

	public void setRunningStatus(boolean runningStatus) {
		this.runningStatus = runningStatus;
	}

}
